package hotswap.ncc;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsl.NCCUtil;
import com.jsl.NCCloud21Service;
import com.jsl.action.OtherOutAndInActionInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.soa.workflow.request.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OtherOutAndInActionInterfaceImpl implements OtherOutAndInActionInterface {

    private static final Log log = LogFactory.getLog(OtherOutAndInActionInterfaceImpl.class);

    @Override
    public String execute(RequestInfo info) {
        log.info("**********************进入 OtherOutAndInActionInterfaceImpl **********************");

        String requestid = info.getRequestid();
        String workflowid = info.getWorkflowid();
        String formtable = NCCUtil.getFormtable(workflowid);
        log.info("[OtherOutAndInActionInterfaceImpl.execute] workflowid:" + workflowid + " formtable:" + formtable + " requestid:" + requestid);

        otherOut(info);
        otherIn(info);

        return null;
    }

    /**
     * 其它出库
     * @param info
     */
    private void otherOut(RequestInfo info) {

        MainTableInfo mainTableInfo = info.getMainTableInfo();
        DetailTableInfo detailTableInfo = info.getDetailTableInfo();

        try {
            // 输出主表参数
            ObjectMapper objectMapper1 = new ObjectMapper();
            String mainTableInfoString = objectMapper1.writeValueAsString(mainTableInfo);
            log.info("[OtherOut.execute] mainTableInfo:" + mainTableInfoString);
            // 输出明细表参数
            ObjectMapper objectMapper2 = new ObjectMapper();
            String detailTableInfoString = objectMapper2.writeValueAsString(detailTableInfo);
            log.info("[OtherOut.execute] detailTableInfo:" + detailTableInfoString);
            List<Map<String, Object>> mapList = new ArrayList<>();

            // 构造请求参数
            Map<String, Object> requestMap = getOtherOutRequestMap(mainTableInfo, detailTableInfo);
            mapList.add(requestMap);
            String requestBody = JSON.toJSONString(mapList);
            log.info("[OtherOut.execute] requestBody:" + requestBody);
            String apiUrl = "/nccloud/api/ic/generalout/save";


            // 拿到返回数据，传入apiUrl、请求参数
            String result = NCCloud21Service.pushData(apiUrl, requestBody);

            log.info("[OtherOut.execute] result:" + result);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    /**
     * 其它入库
     * @param info
     */
    private void otherIn(RequestInfo info) {
        MainTableInfo mainTableInfo = info.getMainTableInfo();
        DetailTableInfo detailTableInfo = info.getDetailTableInfo();

        try {
            // 输出主表参数
            ObjectMapper objectMapper1 = new ObjectMapper();
            String mainTableInfoString = objectMapper1.writeValueAsString(mainTableInfo);
            log.info("[OtherIn.execute] mainTableInfo:" + mainTableInfoString);
            // 输出明细表参数
            ObjectMapper objectMapper2 = new ObjectMapper();
            String detailTableInfoString = objectMapper2.writeValueAsString(detailTableInfo);
            log.info("[OtherIn.execute] detailTableInfo:" + detailTableInfoString);
            List<Map<String, Object>> mapList = new ArrayList<>();

            // 构造请求参数
            Map<String, Object> requestMap = getOtherInRequestMap(mainTableInfo, detailTableInfo);
            mapList.add(requestMap);
            String requestBody = JSON.toJSONString(mapList);
            log.info("[OtherIn.execute] requestBody:" + requestBody);
            String apiUrl = "/nccloud/api/ic/generalinforoa/save";

            // 拿到返回数据，传入apiUrl、请求参数
            String result = NCCloud21Service.pushData(apiUrl, requestBody);

            log.info("[OtherIn.execute] result:" + result);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    private static Map<String, Object> getOtherOutRequestMap(MainTableInfo mainTableInfo, DetailTableInfo detailTableInfo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {

            // 构造主体请求参数
            Map<String, Object> generaloutHMap = new HashMap<>();
            // 项目编码
            String projectCode = "";

            for (Property property : mainTableInfo.getProperty()) {
                String name = property.getName();
                // 公司代号
                if (name.equals("fgs")) {
                    generaloutHMap.put("pk_org", property.getValue());
                }
                // 维修仓库编码（出库）
                if (name.equals("wxckllk")) {
                    // 仓库编码
                    generaloutHMap.put("cwarehouseid", property.getValue());
                }

                // 项目
                if (name.equals("xmllk")) {
                    projectCode = property.getValue();
                    generaloutHMap.put("vdef9", property.getValue());
                }

                // 领料员编码
                if (name.equals("rybm")) {
                    generaloutHMap.put("cbizid", property.getValue());
                }

                // 部门代号
                if (name.equals("bmdh")) {
                    generaloutHMap.put("cdptvid", property.getValue());
                }

            }
            // 出入库类型
            generaloutHMap.put("vtrantypecode", "4I-Cxx-01");
            // 制单人
            generaloutHMap.put("billmaker", "hboa");
            // 制单日期
            generaloutHMap.put("dbilldate",  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

            // 备注
            generaloutHMap.put("vnote", "");


            resultMap.put("ic_generalout_h", generaloutHMap);

            // 构造明细请求参数
            List<Map<String, Object>> mapList = new ArrayList<>();
            DetailTable[] detailTableList = detailTableInfo.getDetailTable();
            DetailTable detailTable1 = detailTableList[0];

            for (int i = 0; i < detailTable1.getRow().length; i++) {
                Row row = detailTable1.getRow(i);
                // 组装表体参数
                Map<String, Object> map = new HashMap<>();

                for (Cell cell : row.getCell()) {
                    if (cell.getName().equals("bz")) {
                        // 行备注
                        map.put("vnotebody", cell.getValue());
                    }
                    if (cell.getName().equals("wlbm")) {
                        // 物料编码
                        map.put("cmaterialvid", cell.getValue());
                    }
                    if (cell.getName().equals("wlsl")) {
                        // 应发数量
                        map.put("nshouldassistnum", cell.getValue());
                        // 实发数量
                        map.put("nnum", cell.getValue());
                    }

                }
                map.put("crowno", (i+1) * 10);
                // 项目编码
                map.put("cprojectid", projectCode);

                // 出库日期
                map.put("dbizdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                mapList.add(map);
            }

            resultMap.put("ic_generalout_b", mapList);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }



    private static Map<String, Object> getOtherInRequestMap(MainTableInfo mainTableInfo, DetailTableInfo detailTableInfo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {

            // 构造主体请求参数
            Map<String, Object> generalinHMap = new HashMap<>();
            // 项目主键
            String projectId = "";

            for (Property property : mainTableInfo.getProperty()) {
                String name = property.getName();
                // 公司代号
                if (name.equals("fgs")) {
                    generalinHMap.put("pk_org", property.getValue());
                }
                // 总仓返修仓编码（入库）
                if (name.equals("fxckllk")) {
                    // 仓库编码
                    generalinHMap.put("cwarehouseid", property.getValue());
                }
                // 项目主键
                if (name.equals("xmllk")) {
                    projectId = property.getValue();
                    generalinHMap.put("vdef9", property.getValue());
                }
                // 制单人编码
                if (name.equals("rybm")) {
                    generalinHMap.put("cbizid", property.getValue());
                }

                // 部门代号
                if (name.equals("bmdh")) {
                    generalinHMap.put("cdptvid", property.getValue());
                }



            }
            // 制单日期
            generalinHMap.put("dbilldate",  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            // 出入库类型
            generalinHMap.put("vtrantypecode", "4A-01");
            // 制单人
            generalinHMap.put("billmaker", "hboa");

            // 备注
            generalinHMap.put("vnote", "");


            resultMap.put("ic_generalin_h", generalinHMap);

            // 构造明细请求参数
            List<Map<String, Object>> mapList = new ArrayList<>();
            DetailTable[] detailTableList = detailTableInfo.getDetailTable();
            DetailTable detailTable1 = detailTableList[0];

            // 返修数量
            for (int i = 0; i < detailTable1.getRow().length; i++) {
                Row row = detailTable1.getRow(i);

                // 组装表体参数
                Map<String, Object> map = new HashMap<>();

                for (Cell cell : row.getCell()) {
                    if (cell.getName().equals("wlbm")) {
                        // 物料编码
                        map.put("cmaterialvid", cell.getValue());
                    }
                    if (cell.getName().equals("wlsl")) {
                        // 应收数量
                        map.put("nshouldassistnum", cell.getValue());
                        // 实收数量
                        map.put("nnum", cell.getValue());
                    }

                    if (cell.getName().equals("bz")) {
                        // 行备注
                        map.put("vnotebody", cell.getValue());
                    }

                    // 单价
                    if (cell.getName().equals("dj")) {
                        map.put("ncostprice", cell.getValue());
                    }

                    if (cell.getName().equals("sbzt")) {
                        // 行号
                        String sbzt = cell.getValue();
                        if("1".equals(sbzt)){
                            map.put("vbdef11", "内部维修");
                        }
                        if("2".equals(sbzt)){
                            map.put("vbdef11", "委外维修");
                        }
                    }
                }
                // 入库日期
                map.put("dbizdate", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                // 项目编码
                map.put("cprojectid", projectId);
                // 行号
                map.put("crowno", (i+1) * 10);


                mapList.add(map);
            }

            resultMap.put("ic_generalin_b", mapList);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }


}
